<?php
namespace app\gym\controller;
use app\common\controller\AdminBase;
/*卡种类*/
class GymCard extends AdminBase{
	private $shop_id;
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();
		$this->shop_id=sp_get_current_shop_id();
    }
	/*列表*/
	function index(){
		echo $this->fetch();
	}	
	//删除
	function delete(){
		$id=input("id");
		db("GymCard")->where("id =$id and shop_id=".$this->shop_id)->update(array("status"=>0,"admin_id"=>sp_get_current_admin_id()));
		$this->ajax_success("成功");
	}
	//编辑
	function edit(){
		$model=db("GymCard")->where("shop_id=".$this->shop_id." and id=".input("id"))->find();
		$this->assign("model",$model);
		echo $this->fetch();	
	}
	function edit_post(){
		if(request()->isPost()){//提交
		    $data=array("card_name"=>input("card_name"),"descs"=>input("descs"),"price"=>input("price",0),"card_type"=>input("card_type"),"admin_id"=>sp_get_current_admin_id());
			if($data['card_type']==1){//次卡
				$data['able_count']=input("able_count",0);
				$data['expiry_date']=input("expiry_date",0);
			}else if($data['card_type']==2){//期限卡
				$data['expiry_date']=input("expiry_date",0);
			}else if($data['card_type']==3){//储值卡
				$data['able_money']=input("able_money",0);
				$data['expiry_date']=input("expiry_date",0);
			}else if($data['card_type']==4){//错峰卡
				$data['time_sets']=input("time_sets");
				$data['expiry_date']=input("expiry_date",0);
			}
			db("GymCard")->where("shop_id=".$this->shop_id." and id=".input("id"))->update($data);
			$this->ajax_success("成功");
		}
	}
	//新增
	function add(){
		if(request()->isPost()){
			db("GymCard")->insert(array(
								"card_name"=>input("card_name"),
								"shop_id"=>$this->shop_id,
								"expiry_date"=>input("expiry_date",0),
								"price"=>input("price",0),
								"descs"=>input("descs"),
								"able_count"=>input("able_count",0),
								"card_type"=>input("card_type"),
								"able_money"=>input("able_money",0),
								"admin_id"=>sp_get_current_admin()["id"],
								"time_sets"=>input("time_sets"),
							));
		    $this->ajax_success("成功");
		}else{
			echo $this->fetch();	
		}
	}
	//tab
	function step(){
		echo $this->fetch();
	}
	
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10051002");
		$this->assign("menus",$auth_group);
	}
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
		 'card_name'=>array("field" =>"card_name","operator" =>"="),
		 'card_type'=>array("field" =>"card_type","operator" =>"="),
		 );
		$shop_id=$this->shop_id;;
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$where_and.=" and status=1 and shop_id=$shop_id";
		$total=db("GymCard")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("GymCard")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
}
